package com.easy.login.controller;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easy.auth.domain.Action;
import com.easy.auth.domain.Role;
import com.easy.auth.domain.Userinfo;
import com.easy.auth.service.ActionService;
import com.easy.auth.service.RoleService;
import com.easy.auth.service.UserinfoService;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.web.message.Message;

@Controller
@RequestMapping("LoginController")
public class LoginController {

	@Autowired
	private UserinfoService userinfoService;

	@Autowired
	private ActionService actionService;
	@Autowired
	private RoleService roleService;
	

	/**
	 * 用户名密码验证
	 * 
	 * @param userinfo
	 * @return
	 */
	@RequestMapping("login")
	@ResponseBody
	public Message login(Userinfo userinfo, HttpServletRequest request) {
		Message msg = new Message();
		msg.setTitle("提示");
		Userinfo user = userinfoService.findUserinfo(userinfo);
		
		if (user != null) {
			PageInfo pageInfo=new PageInfo(99);
			request.getSession().setAttribute("userid", userinfo.getUserid());
			List<Role> roleList = (List<Role>) roleService.getRolesByUserId(user.getOid(), pageInfo).getRows();
			String roleNames="";
			if(roleList!=null){
				for (int i = 0; i < roleList.size(); i++) {
					roleNames=roleList.get(i).getOname()+","+roleNames;
				}
			}
			request.getSession().setAttribute("userid", userinfo.getUserid());
			request.getSession().setAttribute("roleNames", roleNames);
			msg.setSuccessful(true);
			msg.setMsg("通过验证，页面跳转中...");
		} else {
			msg.setMsg("用户名或密码不正确，请重新输入！");
		}
		return msg;
	}
 
	
	/**
	 * 登陆成功后跳转到主页面
	 * 
	 * @return
	 */
	@RequestMapping("goToMainPage")
	public String login(HttpServletRequest request) {

		// 从session中获取userid
		String userid = request.getSession().getAttribute("userid") != null ? request
				.getSession().getAttribute("userid").toString()
				: "";
		// 最终封装menu的数据集
		List<Map<Action, List<Action>>> list = new ArrayList<Map<Action, List<Action>>>();
		// 根据userid获取用户信息
		Userinfo userinfo = new Userinfo();
		userinfo.setUserid(userid);
		userinfo = userinfoService.findUserinfo(userinfo);

		List<Action> firstLevel = actionService.getActionByUserId(
				userinfo.getOid(), "");
		for (Action action : firstLevel) {
			Map<Action, List<Action>> menudata = new HashMap<Action, List<Action>>();
			List<Action> secondLevel = actionService.getActionByUserId(
					userinfo.getOid(), action.getOid());
			menudata.put(action, secondLevel);
			list.add(menudata);
		}

 		request.getSession().setAttribute("menuList", list);
// 		for(Map<Action, List<Action>> map : list){
//			for(Action first : map.keySet()){
//				
//				System.out.println("=========="+map.get(first)==null);
//				if(map.get(first)==null||map.get(first).size()<1){
//					System.out.println(map.get(first).get(0).getId());
//				}}}
		return "main";
	}


	
	/**
	 * 注销
	 * @param request
	 * @return
	 */
	@RequestMapping("logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("userid");
		request.getSession().removeAttribute("roleNames");
		request.getSession().removeAttribute("menuList");
		request.getSession().removeAttribute("menuId");
		request.getSession().removeAttribute("secondMenuId");
		return "login";
	}
	
	@RequestMapping("putMenuIdInto")
	@ResponseBody
	public Message putMenuIdInto(String oid,String soid,HttpServletRequest request){
		request.getSession().setAttribute("menuId", oid);
		request.getSession().setAttribute("secondMenuId", soid);
		//System.out.println("11111111111:"+"redirect:"+request.getContextPath()+url);
		//return "redirect:"+request.getContextPath()+url+"?";
		Message message = new Message();
		message.setSuccessful(true);
		return message;
	}
}
